#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys,urllib,urllib2,re,imp,os
import xbmc,xbmcplugin,xbmcgui
path=xbmc.translatePath(os.path.join(os.path.normpath(os.path.join(os.getcwd(),os.pardir,os.pardir)),'hoster'+os.sep+'oracle'+os.sep+'oracle2.py'))
oracle2=imp.load_source("oracle2", "oracle2.py", open(path,'r'))


#--------------------------------------------------------------------------------------------------
#author:
#elypter
#-mail:elypter@yahoo.de
#
#for details look at readme.txt


#--------------------------------------------------------------------------------------------------
debug=1
dstr="hoster*oracle*interface2*"

def get_verion():
	#or look at the filename
	return 2
	
	
#--------------------------------------------------------------------------------------------------	
"""	
def get_links_by_get_link(array):
	links=[]
	for method,args,item in array:
		links.append((method,method(in args)))
	return links
"""	
def mirrorlist(array):
	MIRROR_LIST(array)
	
def MIRROR_LIST(array):
	for thing in array:
		print str(thing)+";\n"
	for method,args,item in array:
		#item: image,title,filename,hoster
		i=0
		argstr=""
		for arg in args:
			argstr=argstr+"args["+str(i)+"],"
			i=i+1
		execstr="result=oracle2."+method+"("+argstr[:-1]+")"
		
		if debug > 0: print dstr+"MIRROR_LIST* execstr: "+ execstr
		exec execstr
		if debug > 0: print dstr+"MIRROR_LIST* result: "+ str(result)
		for status,link,image,title,filename,captcha,hoster in result:
			if status=="got":
				if len(result)==1:
					ACTION_LIST(str(link),str(item[2]))
				else:
					addDir(str(item[1])+"*"+str(item[2])+"*"+str(hoster), str(link) , 4, str(item[0]))
			elif status=="captcha":
				if len(result)==0:#soll nicht eintreten
					CAPTCHA_LIST(str(link))
				else:
					addDir(str(item[1])+"*"+str(item[2])+"*"+str(hoster), str(link) , 3, str(captcha))
			elif status=="fail":
				print "nothing"
			else:
				if debug > 0: print dstr+"MIRROR_LIST* unknown status: "+ str(status)
				if len(result)==1:
					ACTION_LIST(str(link),str(item[2]))
				else:
					addDir(str(item[1])+"*"+str(item[2])+"*"+str(hoster), str(link) , 4, str(item[0]))
				
def LINK_LIST():
	print "nope"
	
def CAPTCHA_LIST(url):
	print "nope"
	#display link if a captcha was needed and has been accepted
	url1=url
	kb = xbmc.Keyboard('default', 'heading', False)
	kb.setDefault('') # optional
	kb.setHeading('Enter code') # optional
	kb.doModal()
	if (kb.isConfirmed()):
		code = kb.getText()
		
	
	#for arg in args:
	#	argstr=argstr+"args["+str(i)+"],"
	#	i=i+1
	#execstr="result=oracle2."+method+"("+argstr[:-1]+")"
	
	status2,link2,image2,title2,filename2,captcha2,hoster2=oracle2.get_links_by_verification(url,code,[])
	#result=oracle2.get_link2(url,code)
	##rl2=getattr(modules,hoster2).get_link2(url1,code)
	
	
	
	if link2!="" and link2!=None:
		##addLink("Watch on "+hoster2, link2, '')
		ACTION_LIST(link2)
		#addDir(str(item[1])+"*"+str(item[2])+"*"+str(hoster2), str(link2) , 4, str(item[0]))
	else:
		addLink("Captcha code incorrect or other error", link2, '')
	
def ACTION_LIST(url,file):
	if debug > -1: print dstr+"ACTION_LIST('"+str(url)+","+file+"')"
	addLink("Play "+file, str(url), "")
	#play
	#-internal player
	#-vlc
	#-other player
	#download
	#-just download
	#-play after with internal player
	#-play after with vlc
	#-play after with other player
	#-play while with internal player
	#-play while with vlc
	#-play while with other player
	#remove from history
	#if exists: delete download
	#details
	#settings
	
def DOWNLOAD_LIST():
	print "nope"

def HISTORY_LIST():
	print "nope"

def SETTINGS():
	print "nope"

		
#--------------------------------------------------------------------------------------------------	
def addLink(name,url,iconimage):
		ok=True
		liz=xbmcgui.ListItem(name, iconImage="DefaultVideo.png", thumbnailImage=iconimage)
		liz.setInfo( type="Video", infoLabels={ "Title": name } )
		ok=xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]),url=url,listitem=liz)
		return ok


def addDir(name,url,mode,iconimage):
		u=xbmc.translatePath('plugin://hoster/oracle/')+"?url="+urllib.quote_plus(url)+"&mode="+str(mode)+"&name="+urllib.quote_plus(name)
		#u=xbmc.translatePath('plugin://hoster/oracle/')+"?url="+urllib.quote_plus(str(url.encode("utf-8")))+"&mode="+str(mode)+"&name="+urllib.quote_plus(str(name.encode("utf-8")))
		ok=True
		liz=xbmcgui.ListItem(name, iconImage="DefaultFolder.png", thumbnailImage=iconimage)
		liz.setInfo( type="Video", infoLabels={ "Title": name} )
		ok=xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]),url=u,listitem=liz,isFolder=True)
		return ok
		

	
